package com.eduplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eduplatform.common.Result;
import com.eduplatform.common.UserContext;
import com.eduplatform.dto.AIPaperGenerateDTO;
import com.eduplatform.dto.PaperDTO;
import com.eduplatform.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 试卷控制器
 */
@RestController
@RequestMapping("/api/papers")
public class PaperController {

    @Autowired
    private PaperService paperService;

    @Autowired
    private UserContext userContext;

    /**
     * 分页查询试卷列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) Integer educationLevel,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword) {
        IPage<PaperDTO> pageResult = paperService.pagePapers(page, size, subjectId, educationLevel, status, keyword);

        Map<String, Object> data = new HashMap<>();
        data.put("total", pageResult.getTotal());
        data.put("pages", pageResult.getPages());
        data.put("current", pageResult.getCurrent());
        data.put("records", pageResult.getRecords());

        return Result.success(data);
    }

    /**
     * 获取试卷详情
     */
    @GetMapping("/{id}")
    public Result getDetail(@PathVariable Long id) {
        PaperDTO paper = paperService.getPaperDetail(id);
        if (paper == null) {
            return Result.error("试卷不存在");
        }
        return Result.success(paper);
    }

    /**
     * 添加试卷
     */
    @PostMapping("/add")
    public Result add(@RequestBody @Valid PaperDTO paperDTO) {
        Long currentUserId = userContext.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        paperDTO.setCreatorId(currentUserId);
        boolean result = paperService.addPaper(paperDTO);
        return result ? Result.success() : Result.error("添加试卷失败");
    }

    /**
     * 更新试卷
     */
    @PutMapping("/update")
    public Result update(@RequestBody @Valid PaperDTO paperDTO) {
        if (paperDTO.getId() == null) {
            return Result.error("试卷ID不能为空");
        }

        boolean result = paperService.updatePaper(paperDTO);
        return result ? Result.success() : Result.error("更新试卷失败");
    }

    /**
     * 删除试卷
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean result = paperService.deletePaper(id);
        return result ? Result.success() : Result.error("删除试卷失败");
    }

    /**
     * 发布试卷
     */
    @PostMapping("/{id}/publish")
    public Result publish(@PathVariable Long id) {
        paperService.publishPaper(id);
        return Result.success();
    }

    /**
     * 获取AI支持的学科列表
     *
     * @return 支持的学科列表
     */
    @GetMapping("/ai-subjects")
    public Result getAISupportedSubjects() {
        return Result.success(paperService.getAISupportedSubjects());
    }

    /**
     * 使用AI生成试卷
     *
     * @param generateDTO 生成参数
     * @return 生成的试卷
     */
    @PostMapping("/ai-generate")
    public Result generateAIPaper(@RequestBody @Valid AIPaperGenerateDTO generateDTO) {
        Long currentUserId = userContext.getCurrentUserId();
        if (currentUserId == null) {
            return Result.error("用户未登录");
        }
        PaperDTO paperDTO = paperService.generateAIPaper(generateDTO, currentUserId);
        return Result.success(paperDTO);
    }
}